<template>
  <div>
    <el-table :data="nowTableData" border style="width: 100%">
      <el-table-column prop="userHead" label="用户头像">
        <template slot-scope="scope">
          <img class="userHead" :src="scope.row.userHead" />
        </template>
      </el-table-column>
      <el-table-column prop="date" label="注册日期"></el-table-column>
      <el-table-column prop="username" label="用户姓名"></el-table-column>
      <el-table-column prop="email" label="用户邮箱"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">  <!--作用域插槽，拿到下面的信息-->
          <el-button size="small" @click="handleToFreeze(scope.$index,scope.row)">
            {{ scope.row.isFreeze ? '已冻结' : '未冻结' }}
          </el-button>
          <el-button size="small" type="danger" @click="handleToDelete(scope.$index,scope.row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination background layout="prev, pager, next" :total="tableData.length" :page-size="pageSize" class="page"
                   :current-page.sync="currentPage">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "users",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10
    }
  },
  computed: {
    /*todo 内容服务器的分页返回*/
    nowTableData() {
      return this.tableData.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize) || [];
    }
  },
  methods: {
    handleToFreeze(index, row) {
      this.$confirm('请再次确认，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!',
        });
        this.axios.post('/api2/admin/updateFreeze', {
          email: row.email,
          isFreeze: !row.isFreeze
        }).then((res) => {
          var status = res.data.status;
          if (status === 0) {
            this.nowTableData[index].isFreeze = !row.isFreeze;
          } else {
            this.$alert('操作失败', '信息', {
              confirmButtonText: '确定',
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    },
    handleToDelete(index, row) {
      this.$confirm('请再次确认，是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '操作成功!',
        });
        this.axios.post('/api2/admin/deleteUser', {
          email: row.email,
        }).then((res) => {
          var status = res.data.status;
          if (status === 0) {
            this.nowTableData.splice(index, 1);
          } else {
            this.$alert('操作失败', '信息', {
              confirmButtonText: '确定',
            });
          }
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消操作'
        });
      });
    }
  },
  mounted() {
    this.axios.get('/api2/admin/usersList').then((res) => {
      var status = res.data.status;
      if (status === 0) {
        this.tableData = res.data.data.usersList;
      }
    });
  }
}
</script>

<style scoped>
.page {
  margin-top: 20px
}
.userHead{
  width: 50px;
  height: 50px;
  border-radius: 50%;
  overflow: hidden;
}
</style>
